<?php
 
	function smarty_block_stocks($params, $content, &$smarty, &$repeat) {

		global $db, $config, $counter;

		$l = count($smarty->_tag_stack);
		if(empty($params['name'])) $params['name'] = "stocks";

		if(empty($smarty->blockvars[$l])) {
			$sqlstr = "select a.*,b.shopwarningnum,b.canbuyout,b.supplieid,b.pricebuy,b.currency_unit,b.wanttobuy,c.warningnum,d.pic,d.pid,d.status,d.pname from {$config->db_prefix}stocks as a left join {$config->db_prefix}products as b on b.product_sku_id=a.productid left join {$config->db_prefix}classes as c on b.classid=c.id left join haiou_product_sku as d on a.productid=d.id  where 1=1 ";
			if (!empty($params['id'])) $sqlstr .= " and a.id = ".$db->qstr($params['id']);
			if (!empty($params['productid'])) $sqlstr .= " and d.pid in(".$params['productid'].")";
			if (!empty($params['shopid'])) $sqlstr .= " and a.shopid = ".$db->qstr($params['shopid']);
			if (isset($params['keywords'])) $sqlstr .= " and d.pname like ". $db->qstr("%".$params['keywords']."%");
			if ($params['lnumber']!==''&&$params['lnumber']!==NULL) $sqlstr .= " and a.stocks>=". $db->qstr($params['lnumber']);
			if ($params['hnumber']!==''&&$params['hnumber']!==NULL) $sqlstr .= " and a.stocks<=". $db->qstr($params['hnumber']);
			if (!empty($params['barcode'])) $sqlstr .= " and b.barcode=".$db->qstr($params['barcode']);
			
			
			if($_GET['shopwarningnum']==1)$sqlstr .= " and a.stocks > b.shopwarningnum";
			if($_GET['shopwarningnum']==2)$sqlstr .= " and a.stocks <= b.shopwarningnum";
			
			if (!empty($params['cond'])) $sqlstr .= " and ({$params['cond']})";

			if (!empty($params['counter'])) $counter = $db->getrow(str_replace("*", "sum(stocks) number", $sqlstr));
			
			if (empty($params['order'])) $params['order']='id';
			    if (!empty($params['ascdesc']))$params['order'].=' '.$params['ascdesc'];else $params['order'].=' desc'; 
			  $sqlstr .= " order by {$params['order']}";

			$limit = empty($params['limit'])?9999:$params['limit'];
			$page = empty($params['page'])?1:$params['page'];
			$pager = new pager($limit, $page);
			//echo $sqlstr;
			$smarty->blockvars[$l] = $pager->execute($db, $sqlstr);

			//
			if(!$smarty->blockvars[$l]) return $repeat = false;
			foreach ($smarty->blockvars[$l] as $kk=>$vv)
			{
				if(!($smarty->blockvars[$l][$kk]['product_pic'] = @strstr($vv['pic'], ",",true)))
				{
					$smarty->blockvars[$l][$kk]['product_pic'] = $vv['pic'];
				}
				if($vv['supplieid']!=""){
				    $sql = "select * from {$config->db_prefix}suppliers where find_in_set(id,'{$vv['supplieid']}')";
				    $suppliers = $db->GetArray($sql);
				    $smarty->blockvars[$l][$kk]['suppliers'] = $suppliers;
				}else{
				    $smarty->blockvars[$l][$kk]['suppliers'] = array();
				}
				if($vv['shopid']!=""){
				    $sql = "select * from {$config->db_prefix}shops where id={$vv['shopid']} limit 1";
				    $shops = $db->getrow($sql);
				    $smarty->blockvars[$l][$kk]['shop_name'] = $shops['title'];
				}
			}
			if(!empty($params['pager'])) $smarty->assign($params['pager'], $pager);
		}
	
		if(list($key, $item) = each($smarty->blockvars[$l])) {
			$repeat = true;
			
			$pdb = new pager(9999, 1);
				
			$stock = $pdb->execute($db,"select id,productid, lotnumber,timeend ,(number-delnumber)as remain from {$config->db_prefix}purchases where shopid={$item['shopid']} and productid={$item['productid']} and timestocked>0 and number!=delnumber order by timeend asc,timestocked asc");

			
			//采购在途
			
			$purchases=$pdb->execute($db,"select sum(number) as purchases_nums  from {$config->db_prefix}purchases where shopid={$item['shopid']} and productid={$item['productid']} and timestocked=0 and type=0");

			$item['purchases']=$purchases[0]['purchases_nums'];
			
			//预发货
			$orders=$pdb->execute($db,"select sum(number) as orders_nums  from {$config->db_prefix}orders where shopid={$item['shopid']} and productid={$item['productid']} and timestocked=0 and type=0");
				
			$item['orders']=$orders[0]['orders_nums'];
			
			//微仓
			$microstock_nums=$pdb->execute($db,"select sum(num) as microstock_nums  from haiou_micro_stock where stock_shop={$item['shopid']} and sku_id={$item['productid']} and status>0");
			
			$item['microstock_nums']=$microstock_nums[0]['microstock_nums'];
			
			//退货在途
			$reback=$pdb->execute($db,"select sum(number) as reback_nums  from {$config->db_prefix}purchases where shopid={$item['shopid']} and productid={$item['productid']} and timestocked=0 and type=1");
			
			$item['reback']=$reback[0]['reback_nums'];
			
			$item['more_stock']['data']=$stock;
			
			$item['more_stock']['nums']=count($stock);
			if(!empty($counter)) $item["counter"] = $counter;
			
			$smarty->assign($params['name'], $item);
		}
		else {
			$repeat = false;
			$counter = "";
			reset($smarty->blockvars[$l]);
		}
		
		if(!is_null($content)) print $content;
	
		//
		if(!$repeat) $smarty->blockvars[$l] = array();
	}

?>